package demo;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

import java.io.*;
import java.net.SocketException;

public class FTPUtil {
    private static String encoding = System.getProperty("file.encoding");

    private FTPClient ftpClient;

    public FTPUtil(String url,int port,String username,String password) {
        ftpClient = new FTPClient();
        try {
            ftpClient.connect(url,port);
            ftpClient.login(username, password);
            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftpClient.setControlEncoding(encoding);
            ftpClient.enterLocalPassiveMode();
            ftpClient.setBufferSize(1024 * 1024 * 4);
            //FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);//解决中文乱码
            //conf.setServerLanguageCode("zh");
            //ftpClient.configure(conf);
        } catch (SocketException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP连接失败！", e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错！", e);
        }
    }


    public void uploadFile(String src,String des) {
        File srcFile = new File(src);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(srcFile);
            ftpClient.storeFile(des, fis);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("文件未找到！", e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错！", e);
        } finally {
            try {
                if(fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }



    public void downLoadFile(String remote,String local) {
        FileOutputStream fos = null;
        try {
            ftpClient.changeWorkingDirectory(new String(remote.getBytes(),"ISO-8859-1"));
            FTPFile[] files = ftpClient.listFiles();
            for(FTPFile file:files){
                if(file.isDirectory()){
                    File localDir = new File(String.format("%s/%s",local,file.getName()));
                    localDir.mkdir();
                    downLoadFile(String.format("%s/%s",remote,file.getName()),String.format("%s/%s",local,file.getName()));
                }else{
                    System.out.println(file.getName());
                    File localFile = new File(String.format("%s/%s",local,file.getName()));
                    fos = new FileOutputStream(localFile);
                    ftpClient.retrieveFile(new String(file.getName().getBytes(),"ISO-8859-1"),fos);
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            throw new RuntimeException("文件未找到！", e);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错！", e);
        } finally {
            try {
                if(fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public boolean isDirExists(String name) {
        boolean exist = false;
        if(name.isEmpty() || name.length() ==0) {
            return exist;
        }
        try {
            FTPFile[] files = ftpClient.listDirectories();
            for(FTPFile file : files) {
                if(name.equals(file.getName())) {
                    exist = true;
                    return exist;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP客户端出错！", e);
        }
        return exist;
    }
    public void closeConnect() {
        try {
            ftpClient.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("关闭FTP连接发生异常！", e);
        }
    }

    public void cd(String dir) {
        try {
            ftpClient.changeWorkingDirectory(dir);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP发生异常！", e);
        }
    }

    public void mkdir(String dir) {
        try {
            ftpClient.makeDirectory(dir);
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException("FTP发生异常！", e);
        }
    }

    public static void main(String[] args) {
        FTPUtil ftpUtil =new FTPUtil("172.31.1.157",21,"abc","IFLY1.t!@#");
        ftpUtil.downLoadFile("/home/abc/file/train_data","/Users/zwxu/event/temp");
    }
}
